import os
import numpy as np
import datetime

class Translate:

    def __init__(self):
        pass

    def translate(self, result, path = './result', task = 0):
        '''
            task == 0 means all
            task == 1 means travel time
            task == 2 means trafiic volume
        '''

        year = 2016
        month = 10
        day = 25
        time_window = []
        for i in range(7):
            time_window.append([])
            for hour in [8, 9, 17, 18]:
                for minute in [0, 20, 40]:
                    time_1 = datetime.datetime(year, month, day + i, hour, minute, 0, 0).strftime('%Y-%m-%d %H:%M:%S')
                    if minute == 40:
                        time_2 = datetime.datetime(year, month, day + i, hour + 1, 0, 0, 0).strftime('%Y-%m-%d %H:%M:%S')
                    else:
                        time_2 = datetime.datetime(year, month, day + i, hour, minute + 20, 0, 0).strftime('%Y-%m-%d %H:%M:%S')
                    time_window[i].append('"[' + time_1 + ',' + time_2 + ')"')


        if not os.path.exists(path):
            os.makedirs(path)

        if task == 0 or task == 1:
            travel_time_file = open(path + '/travel_time.csv', 'w')
            travel_time_file.write('intersection_id,tollgate_id,time_window,avg_travel_time\n')
            intersection_id = ['A', 'A', 'B', 'B', 'C', 'C']
            tollgate_id = ['2', '3', '1', '3', '1', '3']
            offset = 0
            for i in range(6):
                for j in range(7):
                    for k in range(12):
                        travel_time_file.write(','.join([intersection_id[i], tollgate_id[i], time_window[j][k], str(result[i+offset,j,k])]) + '\n')
        
        if task == 0 or task == 2:
            traffic_volume_file = open(path + '/traffic_volume.csv', 'w')
            traffic_volume_file.write('tollgate_id,time_window,direction,volume\n')
            tollgate_id = ['1', '1', '2', '3', '3']
            direction = ['0', '1', '0', '0', '1']
            offset = 0 if task == 2 else 6
            for i in range(5):
                for j in range(7):
                    for k in range(12):
                        traffic_volume_file.write(','.join([tollgate_id[i], time_window[j][k], direction[i], str(result[i+offset,j,k])]) + '\n')

if __name__ == '__main__':
    tr = Translate()
    tr.translate(np.load('./method1/prediction.npy'), task = 1)
